<?php

App::uses('AppController', 'Controller');

/**
 * BlogsCategorias Controller
 *
 * @property BlogsCategoria $BlogsCategoria
 * @property PaginatorComponent $Paginator
 */
class EmailController extends AppController {

    /**
     * __construct
     *
     * @var mixed
     * @var mixed
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->set('title', __d('emails', 'E-mails'));
    }

    /* --------------------------------------     Public    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function enviar() {
        $dados = $this->request->data;
        App::uses('CakeEmail', 'Network/Email');
        $Email = new CakeEmail();
        $Email->helpers(array('Html'));
        $Email->from(array(Configure::read('Voxel.Email.Email') => Configure::read('Voxel.Config.Titulo') . ' - Contato'));
        $Email->to(($dados['Contato']['email'] != '' ? $dados['Contato']['email'] : Configure::read('Voxel.Email.Email')));
        $Email->bcc(Configure::read('Voxel.Email.Email'));
        $Email->subject(Configure::read('Voxel.Config.Titulo') . ' - Contato');
        $Email->template('default', 'default');
        $Email->viewVars(array('dados' => $dados['Contato']));
        $Email->emailFormat('html');
        $Email->config(
                array(
                    'transport' => 'Smtp',
                    'host' => Configure::read('Voxel.Email.Host'),
                    'port' => Configure::read('Voxel.Email.Port'),
                    'timeout' => 30,
                    'usuarioname' => Configure::read('Voxel.Email.Email'),
                    'replyTo' => Configure::read('Voxel.Email.Email'),
                    'password' => Configure::read('Voxel.Email.Password'),
                    'tls' => (Configure::read('Voxel.Email.Tls') == 1 ? true : false)
                )
        );
        if (!$Email->send()) {
            $this->Session->setFlash(__('Não foi possivel enviar o e-mail.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-danger'
            ));
        } else {
            $this->Session->setFlash(__('E-mail enviado com sucesso.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-success'
            ));
        }

        $this->redirect($this->referer());
    }

}
